package cn.xyy.system.service;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.xyy.system.dao.PermissionDao;
import cn.xyy.system.dao.RoleDao;
import cn.xyy.common.domain.system.auth.Permission;
import cn.xyy.common.domain.system.auth.RolePermission;
import cn.xyy.common.utils.BeanUtils;
import cn.xyy.common.utils.StringUtils;
import cn.xyy.common.exception.AuthException;

/**
 * 权限业务层
 * 
 * @author xyy
 *
 */
@Service
public class PermissionService {

	@Autowired
	private PermissionDao permissionDao;
	@Autowired
	private RoleDao roleDao;

	public List<Permission> getPermissionList() {
		return permissionDao.selectAll();
	}

	public List<Permission> getEnableDeptList() {
		return getPermissionList().stream().filter(e -> 1 == e.getStatus()).collect(Collectors.toList());
	}

	public List<Permission> getPermissionListByRole(Integer[] roleIds) {
		if (roleIds == null) {
			throw new AuthException("角色roleIds为空");
		}
		List<Permission> owendRightList = permissionDao.selectByRole(roleIds);
		if (owendRightList == null || owendRightList.isEmpty()) {
			return null;
		}
		return owendRightList.stream().distinct().collect(Collectors.toList());
	}

	public boolean add(Permission permission) {
		if (permission.getTypes() == null) {
			throw new AuthException("权限类型不能为空");
		}
		if (StringUtils.isBlank(permission.getTitle())) {
			throw new AuthException("菜单名称/按钮名称不能为空");
		}
		if (permission.getTypes() != 2) {
			if (StringUtils.isBlank(permission.getPath())) {
				throw new AuthException("路由地址不能为空");
			}
		}
		if (permission.getTypes() == 1 && permission.getIframe() == 0) {
			if (StringUtils.isBlank(permission.getName())) {
				throw new AuthException("组件名不能为空");
			}
			if (StringUtils.isBlank(permission.getComponent())) {
				throw new AuthException("组件路径不能为空");
			}
		}

		if (permission.getParentId() == null) {
			permission.setParentId(0);
		}
		if (permission.getTypes() == null) {
			permission.setTypes(2);
		}
		if (permission.getHide() == null) {
			permission.setParentId(2);
		}
		if (permission.getHideChildren() == null) {
			permission.setHideChildren(2);
		}
		if (permission.getIframe() == null) {
			permission.setIframe(2);
		}
		if (permission.getStatus() == null) {
			permission.setStatus(1);
		}
		if (permission.getCache() == null) {
			permission.setCache(2);
		}

		int result = permissionDao.insert(permission);
		if (result < 0) {
			throw new AuthException("操作失败");
		}
		return true;
	}

	public boolean update(Permission permission) {
		if (permission.getTypes() == null) {
			throw new AuthException("权限类型不能为空");
		}
		if (StringUtils.isBlank(permission.getTitle())) {
			throw new AuthException("菜单标题/按钮名称不能为空");
		}
		if (permission.getTypes() != 2) {
			if (StringUtils.isBlank(permission.getPath())) {
				throw new AuthException("路由地址不能为空");
			}
		}
		if (permission.getTypes() == 1 && permission.getIframe() == 0) {
			if (StringUtils.isBlank(permission.getName())) {
				throw new AuthException("组件名不能为空");
			}
			if (StringUtils.isBlank(permission.getComponent())) {
				throw new AuthException("组件路径不能为空");
			}
		}
		if (permission.getParentId() == null) {
			permission.setParentId(0);
		}
		Permission permissionInfo = permissionDao.select(permission.getId());
		if (permissionInfo == null) {
			throw new AuthException("没有对应的权限");
		}
		BeanUtils.copyProperties(permission, permissionInfo);
		int result = permissionDao.update(permissionInfo);
		if (result < 0) {
			throw new AuthException("执行失败");
		}
		return true;
	}

	@Transactional(value = "baseTransactionManager")
	public boolean delete(Integer id) {
		if (id == null) {
			throw new AuthException("删除条件不能为空");
		}
		// 删除角色权限
		roleDao.delete(new RolePermission().setPermId(id));

		// 删除权限本身
		permissionDao.delete(new Permission().setId(id));

		return true;
	}

}
